﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Reflection;
using System.Drawing.Drawing2D;
using System.Windows.Forms.VisualStyles;

namespace LED_Editor.Controls
{
    public partial class TracksDataGridView : KryptonDataGridView
    {
        public TracksDataGridView()
        {
            InitializeComponent();
            this.Rows.CollectionChanged += new System.ComponentModel.CollectionChangeEventHandler(Rows_CollectionChanged);
            this.RowStateChanged += new DataGridViewRowStateChangedEventHandler(TracksDataGridView2_RowStateChanged);

        }

      

        void TracksDataGridView2_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            Console.WriteLine(e.StateChanged);
        }

        void Rows_CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
        {
            Console.WriteLine(e.Action);
        }
       
     


        //protected override Sc 
        public ScrollBar VScrollBar
        {
            get
            {
                return VerticalScrollBar;
            }
        }
        //public int verticalOffset;
        private PropertyInfo _verticalOffsetProperty;
        private PropertyInfo VerticalOffsetProperty
        {
            get { if (  _verticalOffsetProperty==null)

            {
                _verticalOffsetProperty = typeof(DataGridView ).GetProperty("VerticalOffset", BindingFlags.Instance | BindingFlags.NonPublic |BindingFlags.IgnoreCase  );      
            }
            return _verticalOffsetProperty;
            }
        }
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden  ) ]
        public int VerticalOffset
        {
            get
            {

                return (int)this.VerticalOffsetProperty.GetValue(this,null) ;
            }
            set
            {
                this.VerticalOffsetProperty.SetValue(this, value, null);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {
 
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
             try
            {
                            base.OnPaintBackground(pevent);

            }
            catch
            {
 
            }
        }

        //protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        //{
        //    //return;
        //    //TextBoxRenderer.DrawTextBox(e.Graphics, e.CellBounds, TextBoxState.Normal);
        //    //e.PaintParts = DataGridViewPaintParts.Border;

        //    //DataGridViewPaintParts parts = DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Background | DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.ErrorIcon;

        //    //DataGridViewPaintParts parts2 = DataGridViewPaintParts.All ;

        //    //DataGridViewCellPaintingEventArgs args = new DataGridViewCellPaintingEventArgs(this, e.Graphics, e.ClipBounds, e.CellBounds, e.RowIndex, e.ColumnIndex, e.State, e.Value, e.FormattedValue, e.ErrorText, e.CellStyle, e.AdvancedBorderStyle, parts);
        //    //args.PaintContent(e.ClipBounds);
        //    //args.Handled = true;
        //    //base.OnCellPainting(args);
        //    //e.PaintContent(e.ClipBounds);
        //    //e.PaintBackground (e.ClipBounds,true);
        //    //e.Paint(e.ClipBounds, parts2);
        //    //e.Handled = true;

        //    //DataGridViewPaintParts parts = DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Background | DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.ErrorIcon;
        //    //DataGridViewCellPaintingEventArgs args = new DataGridViewCellPaintingEventArgs(this, e.Graphics, e.ClipBounds, e.CellBounds, e.RowIndex, e.ColumnIndex, e.State, e.Value, e.FormattedValue, e.ErrorText, e.CellStyle, e.AdvancedBorderStyle, e.PaintParts);
        //    //base.OnCellPainting(args);

        //    //base.OnCellPainting(e);

        //    IPaletteBack back;
        //    IPaletteBorder border;
        //    IPaletteContent content;
        //    PaletteState state = this.GetCellTriple(e.State, e.RowIndex, e.ColumnIndex, out back, out border, out content);
        //     //Palette.  

        //    Rectangle clipRect = new Rectangle(0, 0, e.CellBounds.Width, e.CellBounds.Height);

        //    using (RenderContext context = new RenderContext(this, e.Graphics, clipRect, base.Renderer))
        //    {
        //        Renderer.RenderStandardBorder.DrawBorder(context, e.CellBounds, border, VisualOrientation.Top, state);
        //       // Renderer.RenderStandardContent.DrawContent(context, e.CellBounds, content, new Timer(),  VisualOrientation.Top, false, state,true,true);
        //        using (ViewLayoutContext context2 = new ViewLayoutContext(this, this.Renderer))
        //        {
        //            using (IDisposable disposable = this.Renderer.RenderStandardContent.LayoutContent(context2, clipRect, content, new ButtonValues(this.NeedPaintDelegate), VisualOrientation.Top, false, state, false))
        //            {
        //                Renderer.RenderStandardContent.DrawContent(context, clipRect, content, disposable, VisualOrientation.Top, false, state, false, true);

        //                GraphicsPath path = this.Renderer.RenderStandardBorder.GetBackPath(context, clipRect, border, VisualOrientation.Top, state);
        //                Renderer.RenderStandardBack.DrawBack(context, clipRect, path, back, VisualOrientation.Top, state, null);

        //            }
        //        }



        //    }
        //    return;
        //    try
        //    {
        //       // int height = this.bb.GetContentShortTextFont(state).Height;
        //    }
        //    catch
        //    {
        //       // this.o();
        //    }
        //   // bool rtl = this.aa();
        //    using (Bitmap bitmap = new Bitmap(e.CellBounds.Width, e.CellBounds.Height, e.Graphics))
        //    {
        //        using (Graphics graphics = Graphics.FromImage(bitmap))
        //        {
        //            using (RenderContext context = new RenderContext(this, graphics, clipRect, base.Renderer))
        //            {
        //                //  this.StateCommon.Background.SetInherit(border);
        //               //this.StateCommon.MaxBorderEdges = this.a(e.CellBounds, e.ColumnIndex, e.RowIndex);
        //                Padding padding = this.Renderer.RenderStandardBorder.GetBorderRawPadding(border, state, VisualOrientation.Top);
        //                //GraphicsPath path = this.Renderer.RenderStandardBorder.GetBackPath(context, clipRect, border, VisualOrientation.Top, state);
        //                Rectangle rect = CommonHelper.ApplyPadding(VisualOrientation.Top, clipRect, padding);
        //                //this.ba.a(back, e.CellStyle);
        //                //this.Renderer.RenderStandardBack.DrawBack(context, rect, path, back, VisualOrientation.Top, state, null);
        //                this.Renderer.RenderStandardBorder.DrawBorder(context, clipRect, border, VisualOrientation.Top, state);
        //                //path.Dispose();
        //                if ((e.RowIndex == -1) && (e.ColumnIndex >= 0))
        //                {
        //                    if (base.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection != SortOrder.None)
        //                    {
        //                        //clipRect = this.y.RenderGlyph.DrawGridSortGlyph(context, base.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection, clipRect, content, state, rtl);
        //                    }
        //                }
        //                else if ((e.RowIndex >= 0) && (e.ColumnIndex == -1))
        //                {
        //                    GridRowGlyph none = GridRowGlyph.None;
        //                    if (base.CurrentCellAddress.Y == e.RowIndex)
        //                    {
        //                        if (base.VirtualMode)
        //                        {
        //                            if (base.IsCurrentRowDirty && base.ShowEditingIcon)
        //                            {
        //                                none = GridRowGlyph.Pencil;
        //                            }
        //                            else if (base.NewRowIndex == e.RowIndex)
        //                            {
        //                                none = GridRowGlyph.ArrowStar;
        //                            }
        //                            else
        //                            {
        //                                none = GridRowGlyph.Arrow;
        //                            }
        //                        }
        //                        else if (base.IsCurrentCellDirty && base.ShowEditingIcon)
        //                        {
        //                            none = GridRowGlyph.Pencil;
        //                        }
        //                        else if (base.NewRowIndex == e.RowIndex)
        //                        {
        //                            none = GridRowGlyph.ArrowStar;
        //                        }
        //                        else
        //                        {
        //                            none = GridRowGlyph.Arrow;
        //                        }
        //                    }
        //                    else if (base.NewRowIndex == e.RowIndex)
        //                    {
        //                        none = GridRowGlyph.Star;
        //                    }
        //                    if (none != GridRowGlyph.None)
        //                    {
        //                        //clipRect = this.y.RenderGlyph.DrawGridRowGlyph(context, none, clipRect, content, state, rtl);
        //                    }
        //                    if (base.ShowRowErrors && !string.IsNullOrEmpty(base.Rows[e.RowIndex].ErrorText))
        //                    {
        //                        //Rectangle rectangle3 = clipRect;
        //                        //clipRect = this.y.RenderGlyph.DrawGridErrorGlyph(context, clipRect, state, rtl);
        //                        //Rectangle rectangle4 = new Rectangle(clipRect.Right + 1, clipRect.Top, rectangle3.Width - clipRect.Width, clipRect.Height);
        //                        //if (this.bd.ContainsKey(e.RowIndex))
        //                        //{
        //                        //    this.bd[e.RowIndex] = rectangle4;
        //                        //}
        //                        //else
        //                        //{
        //                        //    this.bd.Add(e.RowIndex, rectangle4);
        //                        //}
        //                    }
        //                    //else if (this.bd.ContainsKey(e.RowIndex))
        //                    //{
        //                    //    this.bd.Remove(e.RowIndex);
        //                    //}
        //                }
        //                else if (((e.RowIndex >= 0) && (e.ColumnIndex >= 0)) && (base.ShowCellErrors && !string.IsNullOrEmpty(base.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText)))
        //                {
        //                   //clipRect = this.y.RenderGlyph.DrawGridErrorGlyph(context, clipRect, state, rtl);
        //                }
        //                if (((e.PaintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) || ((e.PaintParts & DataGridViewPaintParts.ContentBackground) == DataGridViewPaintParts.ContentBackground))
        //                {
        //                    if ((e.ColumnIndex >= 0) && (e.RowIndex >= 0))
        //                    {
        //                        e.Graphics.DrawImage(bitmap, e.CellBounds.Location);
        //                        e.Paint(e.ClipBounds, e.PaintParts & (DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground));
        //                    }
        //                    else
        //                    {
        //                        //this.bb.a(content, e.CellStyle);
        //                        if (e.FormattedValue != null)
        //                        {
        //                           // this.a7.a(e.FormattedValue.ToString());
        //                            using (ViewLayoutContext context2 = new ViewLayoutContext(this, this.Renderer))
        //                            {
        //                                if ((e.RowIndex == -1) && (e.ColumnIndex != -1))
        //                                {
        //                                    Size size = this.Renderer.RenderStandardContent.GetContentPreferredSize(context2, content, new ButtonValues(this.NeedPaintDelegate) , VisualOrientation.Top, false, state, false);
        //                                    //bool flag2 = (size.Width <= clipRect.Width) && (size.Height <= clipRect.Height);
        //                                    //if (this.bc.ContainsKey(e.ColumnIndex))
        //                                    //{
        //                                    //    this.bc[e.ColumnIndex] = flag2;
        //                                    //}
        //                                    //else
        //                                    //{
        //                                    //    this.bc.Add(e.ColumnIndex, flag2);
        //                                    //}
        //                                }
        //                                using (IDisposable disposable = this.Renderer.RenderStandardContent.LayoutContent(context2, clipRect, content , new ButtonValues(this.NeedPaintDelegate), VisualOrientation.Top, false, state, false))
        //                                {
        //                                    this.Renderer.RenderStandardContent.DrawContent(context, clipRect, content, disposable, VisualOrientation.Top, false, state, false, true);
        //                                }
        //                            }
        //                        }
        //                        e.Graphics.DrawImage(bitmap, e.CellBounds.Location);
        //                    }
        //                }
        //                else
        //                {
        //                    e.Graphics.DrawImage(bitmap, e.CellBounds.Location);
        //                }
        //            }
        //        }
        //    }
        //    if (((((e.PaintParts & DataGridViewPaintParts.Focus) == DataGridViewPaintParts.Focus) && this.ShowFocusCues) && (this.Focused && (e.ColumnIndex >= 0))) && (((e.RowIndex >= 0) && (base.CurrentCellAddress.X == e.ColumnIndex)) && (base.CurrentCellAddress.Y == e.RowIndex)))
        //    {
        //        Rectangle cellBounds = e.CellBounds;
        //        cellBounds.Width--;
        //        cellBounds.Height--;
        //        //if (rtl)
        //        //{
        //        //    cellBounds.X++;
        //        //}
        //       // ControlPaint.DrawFocusRectangle(e.Graphics, cellBounds, System.Drawing.Color.Empty, content.GetContentShortTextColor1(state));
        //    }
        //    e.Handled = true;
        //    //base.OnCellPainting(e);


        //}
        private MethodInfo _OnCellPainting; 


        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (_OnCellPainting == null)
            {
               _OnCellPainting =  typeof(DataGridView).GetMethod("OnCellPainting",BindingFlags.NonPublic | BindingFlags.Instance   );
            }
            //e.Graphics.DrawLine(Pens.Black, e.CellBounds.Location.X, e.CellBounds.Location.Y, e.CellBounds.Location.X + e.CellBounds.Width, e.CellBounds.Location.Y);
            //e.Graphics.DrawLine(Pens.White, e.CellBounds.Location.X, e.CellBounds.Location.Y, e.CellBounds.Location.X + e.CellBounds.Width, e.CellBounds.Location.Y);
            //e.PaintContent(e.ClipBounds);
           // e.Paint(e.ClipBounds,DataGridViewPaintParts.All  );
            //Renderer.RenderStandardContent.DrawContent(    

            //IPaletteBack back;
            //IPaletteBorder border;
            //IPaletteContent content;
            //PaletteState state = this.GetCellTriple(e.State, e.RowIndex, e.ColumnIndex, out back, out border, out content);
            ////Palette.  

            //Rectangle clipRect = new Rectangle(0, 0, e.CellBounds.Width, e.CellBounds.Height);

            //using (RenderContext context = new RenderContext(this, e.Graphics, clipRect, this.Renderer))
            //{
                //Renderer.RenderStandardBorder.    
             //   ((PaletteBorder)border).DrawBorders = PaletteDrawBorders.Top | PaletteDrawBorders.Left  ; // DrawBorders  
            //    Renderer.RenderStandardBorder.DrawBorder(context, e.CellBounds, border, VisualOrientation.Left , state);
            //}
            //    // Renderer.RenderStandardContent.DrawContent(context, e.CellBounds, content, new Timer(),  VisualOrientation.Top, false, state,true,true);
            //    using (ViewLayoutContext context2 = new ViewLayoutContext(this, this.Renderer))
            //    {
            //        using (IDisposable disposable = this.Renderer.RenderStandardContent.LayoutContent(context2, clipRect, content, new ButtonValues(this.NeedPaintDelegate), VisualOrientation.Top, false, state, false))
            //        {
            //            Renderer.RenderStandardContent.DrawContent(context, clipRect, content, disposable, VisualOrientation.Top, false, state, false, true);

            //            GraphicsPath path = this.Renderer.RenderStandardBorder.GetBackPath(context, clipRect, border, VisualOrientation.Top, state);
            //            Renderer.RenderStandardBack.DrawBack(context, clipRect, path, back, VisualOrientation.Top, state, null);

            //        }
            //    }



            //}
            //if ((e.ColumnIndex >= 0) && (e.RowIndex >= 0))
            //{
                //e.Graphics.DrawImage(bitmap, e.CellBounds.Location);
            //DataGridViewPaintParts parts = DataGridViewPaintParts.Border | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Background | DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.ErrorIcon;

           //e.PaintContent (e.ClipBounds);
           //e.Paint(e.ClipBounds, parts);
            //}
            //e.Handled = true;
            try
            {
               // _OnCellPainting.Invoke(this,new object[]{e});
              // base. OnCellPainting(e);
                  e.Paint(e.ClipBounds, DataGridViewPaintParts.All) ;
                  //e.PaintContent (e.ClipBounds);


            }
            catch
            {

            }
        }
    }
}
